// pages/subpackage/pages/crm/codeEdit/codeEdit.js
var app = getApp();
var formUp = true;
var myData = new Date();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSurvey: false, //是否是勘测工单
    go_home_Date: '', //上门日期
    timeArr: [{ time_id: "", format: "请选择时间段" }], //时间段数组
    timeIndex: 0, //上门时间段下标
    sceneArr: [{ id: "", name: "请选择服务场景" }], //服务场景数组
    sceneIndex: 0, //服务场景下标
    cookertypeArr: [{ id: "", name: "请选择炉具类型" }], //炉具类型数组
    cookertypeIndex: 0, //炉具类型下标
    cooker_purchase_time: '', // 炉具购买时间
    code_id: '', //户号

    codeDetail: {}, //户号详情
    today_data: myData.getFullYear() + '-' + (myData.getMonth() + 1) + '-' + myData.getDate(),//获取今天日期
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    if (options.code_id) {
      that.setData({
        code_id: options.code_id
      })
      that.getCodeInfo(options.code_id);
    }
    // 初始化动画变量
    that.headid = this.selectComponent("#addressid"); //引入地址
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  //打开地图选择
  openAddress() {
    var that = this;
    this.headid.startAddressAnimation(true);
  },
  //确定接受
  getSure(data) {
    console.log('确定事件获取地址', data);
    var that = this;
    let [province_code, city_code, area_code] = data.detail.areaId;
    let [province, city, area] = data.detail.areaName;
    that.setData({
      getAreaMsg: data.detail
    })
  },
  //打开地图选择
  openNewAddress() {
    var that = this;
    this.headidnew.startAddressAnimation(true);
  },
  //确定接受
  getNewSure(data) {
    console.log('确定事件获取地址', data);
    var that = this;
    let [province_code, city_code, area_code] = data.detail.areaId;
    let [province, city, area] = data.detail.areaName;
    that.setData({
      moveAMsg: data.detail
    })
  },


  //获取户号详情
  getCodeInfo(code_id) {
    var that = this;
    app.ajax({
      url: 'api/Equipmentlists/getCodeInfo',
      url_type: 2,
      data: {
        code_id
      },
      success: function(res) {
        if (res.data.code == 1000) {
          var codeDetail = res.data.data;
          that.setData({
            codeDetail: codeDetail,
            cooker_purchase_time: codeDetail.cooker_purchase_time,
            getAreaMsg: {
              areaName: [codeDetail.province, codeDetail.city, codeDetail.area],
              areaId: [codeDetail.province_code, codeDetail.city_code, codeDetail.area_code]
            },
            valueName: [codeDetail.province, codeDetail.city, codeDetail.area],
          })
          that.getSceneList(codeDetail.scene_type); //获取服务场景列表
          that.getCookertypeList(codeDetail.cooker_type); //获取炉具类型列表

        } else {
          app.showToast(res.data.msg);
        }
      }
    })
  },

  //获取服务场景列表
  getSceneList(id) {
    let that = this;
    app.ajax({
      url: 'api/Common/getSearchConfig',
      url_type: 2,
      data: { config_name: 'scene_type' },
      success: function (res) {
        let sceneIndex = that.data.sceneIndex;
        let tempArr = that.data.sceneArr;
        tempArr = tempArr.concat(res.data.data);
        tempArr.forEach(function (item, index) {
          if (id == item.id) {
            sceneIndex = index
          }
        });
        that.setData({
          sceneArr: tempArr,
          sceneIndex: sceneIndex
        })
      }
    })
  },
  //选择服务场景
  sceneChange(e) {
    this.setData({
      sceneIndex: e.detail.value
    })
  },
  //获取炉具类型列表
  getCookertypeList(id) {
    let that = this;
    app.ajax({
      url: 'api/Common/getSearchConfig',
      url_type: 2,
      data: { config_name: 'cooker_type' },
      success: function (res) {
        let cookertypeIndex = that.data.cookertypeIndex;
        let tempArr = that.data.cookertypeArr;
        tempArr = tempArr.concat(res.data.data);
        tempArr.forEach(function (item, index) {
          if (id == item.id) {
            cookertypeIndex = index
          }
        });
        that.setData({
          cookertypeArr: tempArr,
          cookertypeIndex: cookertypeIndex
        })
      }
    })
  },
  //选择炉具类型
  cookertypeChange(e) {
    this.setData({
      cookertypeIndex: e.detail.value
    })
  },
  //选择炉具购买时间
  cookerPurchaseTimeChange(e) {
    this.setData({
      cooker_purchase_time: e.detail.value
    })
  },

  // 检测表单
  checkForm(isTrue, message) {
    if (isTrue) {
      return { isReturn: isTrue, message }
    } else {
      return false;
    }
  },
  //取消
  cancelFun() {
    wx.navigateBack({
      delta: 1
    })
  },
  //提交的表单数据 api/Workorder/add
  formSubmit(e) {
    var that = this,
      pageData = that.data,
      codeDetail = that.data.codeDetail,
      formData = e.detail.value;
    let { isReturn, message } =
      that.checkForm(pageData.sceneIndex <= 0, '请选择服务场景') ||
      that.checkForm(pageData.cookertypeIndex <= 0, '请选择炉具类型') ||
      that.checkForm(!formData.cooker_brand, '请输入炉具品牌') ||
      that.checkForm(!formData.cooker_model_number, '请输入炉具型号') ||
      that.checkForm(!formData.cooker_num, '请输入炉具数量') ||
      that.checkForm(!formData.expected_renovation_num, '请输入预计改造数量') ||
      that.checkForm(!formData.gas_fee, '请输入近三月燃气均价') ||
      that.checkForm(!pageData.cooker_purchase_time, '请选择炉具购买时间') ||
      that.checkForm(pageData.getAreaMsg.length == 0, '请选户号地址') ||
      that.checkForm(!formData.address, '请输入详细地址');
    if (isReturn) {
      app.showToast(message);
      return;
    }

    var submitData = {
      code_id: pageData.code_id,
      code_name: formData.code_name,
      payment_amount: formData.payment_amount,
      province_name: pageData.getAreaMsg ? pageData.getAreaMsg.areaName[0] : '',
      city_name: pageData.getAreaMsg ? pageData.getAreaMsg.areaName[1] : '',
      area_name: pageData.getAreaMsg ? pageData.getAreaMsg.areaName[2] : '',
      province_code: pageData.getAreaMsg ? pageData.getAreaMsg.areaId[0] : '',
      city_code: pageData.getAreaMsg ? pageData.getAreaMsg.areaId[1] : '',
      area_code: pageData.getAreaMsg ? pageData.getAreaMsg.areaId[2] : '',
      address: formData.address,
      scene_type: pageData.sceneArr[pageData.sceneIndex].id, //服务场景
      cooker_type: pageData.cookertypeArr[pageData.cookertypeIndex].id, //炉具类型
      cooker_brand: formData.cooker_brand, //炉具品牌
      cooker_model_number: formData.cooker_model_number, //炉具型号
      cooker_num: formData.cooker_num, //炉具数量
      expected_renovation_num: formData.expected_renovation_num, //预计改造数量
      gas_fee: formData.gas_fee, //近三月燃气均价
      cooker_purchase_time: pageData.cooker_purchase_time, //炉具购买时间
    }

    console.log('提交的表单数据', submitData);
    if (!formUp) {
      app.showToast('请勿重复提交该表单');
      return false;
    }
    formUp = false;
    app.ajax({
      url: 'api/Equipmentlists/edit_code',
      url_type: 2,
      data: submitData,
      success: function(res) {
        if (res.data.code == 1000) {
          app.showToast(res.data.msg);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        } else {
          app.showToast(res.data.msg);
        }

        setTimeout(function() {
          formUp = true;
        }, 600);
      }
    })
  },

})
